package com.yicong.ants.ui.video;

import android.view.View;

import androidx.recyclerview.widget.RecyclerView;

import com.cchao.simplelib.core.CollectionHelper;
import com.cchao.simplelib.core.RxHelper;
import com.cchao.simplelib.ui.activity.BaseStatefulActivity;
import com.dueeeke.videoplayer.player.VideoView;
import com.yicong.ants.Constants;
import com.yicong.ants.R;
import com.yicong.ants.bean.common.MultiItemWrapper;
import com.yicong.ants.bean.video.VideoListBean;
import com.yicong.ants.databinding.VideoShareActivityBinding;
import com.yicong.ants.manager.http.RetrofitHelper;
import com.yicong.ants.ui.video.adapter.VideoListAdapter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 分享过来的单一播放页
 */
public class VideoSharedActivity extends BaseStatefulActivity<VideoShareActivityBinding> implements View.OnClickListener {
    RecyclerView mRecyclerView;
    VideoListAdapter mAdapter;
    VideoView mVideoView;
    String mVid;

    @Override
    protected int getLayout() {
        return R.layout.video_share_activity;
    }

    @Override
    protected void initEventAndData() {
        mDataBind.setClick(this);
        mRecyclerView = mDataBind.recycler;
        mVid = getIntent().getStringExtra(Constants.Extra.Vid);
        initAdapter();
        onLoadData();
        mDataBind.adViewTimer.startTime(mDisposable);
    }

    @Override
    protected void onLoadData() {
        switchView(LOADING);
        Map<String, String> params = new HashMap<>();
        params.put("vid", mVid);
        addSubscribe(RetrofitHelper.getApis().getVideoDetail(params)
            .compose(RxHelper.toMain())
            .subscribe(respBean -> {
                if (respBean.isCodeFail()) {
                    showToast(respBean.getMsg());
                    switchView(NET_ERROR);
                    return;
                }
                switchView(CONTENT);
                List<VideoListBean> list = Collections.singletonList(respBean.getData());
                List<MultiItemWrapper> multiItemWrappers = new ArrayList<>();
                if (CollectionHelper.isNotEmpty(list)) {
                    for (VideoListBean videoListBean : list) {
                        multiItemWrappers.add(MultiItemWrapper.of(VideoListAdapter.NORMAL_VIDEO, videoListBean));
                    }
                }
                mAdapter.setNewData(multiItemWrappers);
                mAdapter.loadMoreEnd();
            }, RxHelper.getSwitchErrorConsumer(this)));
    }

    void initAdapter() {
        mAdapter = new VideoListAdapter(this, thisActivity, mDisposable).setVid(mVid);

        mRecyclerView.setLayoutManager(mAdapter.getViewPagerLayoutManager());
        mAdapter.bindToRecyclerView(mRecyclerView);
        mRecyclerView.setAdapter(mAdapter);
        mVideoView = mAdapter.mVideoView;
        mAdapter.setEnableLoadMore(false);
    }

    @Override
    public void onPause() {
        super.onPause();
        if (mVideoView != null) {
            mVideoView.pause();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mVideoView != null) {
            mVideoView.release();
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.close:
                finish();
                break;
        }
    }
}
